package com.my.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import com.my.domain.entity.Announcement;
import com.my.mapper.AnnouncementMapper;
import com.my.result.Result;
import com.my.service.AnnouncementService;
import com.my.utils.AccountProfile;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 公告表(Announcement)表服务实现类
 *
 * @author makejava
 * @since 2022-04-14 18:13:58
 */
@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement> implements AnnouncementService {

    @Resource
    AnnouncementMapper announcementMapper;

    @Override
    public List<Announcement> getAnnouncemnetById() {
        Subject subject = SecurityUtils.getSubject();
        AccountProfile currentUser = (AccountProfile)subject.getPrincipal();// 拿到AccountProfile对象
        LambdaQueryWrapper<Announcement> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Announcement::getUserId,currentUser.getId());
        Page<Announcement> page = new Page<>(1,5);
        page(page,lambdaQueryWrapper);
        List<Announcement> announcements = page.getRecords();
        return announcements;
    }

    @Override
    public Result updataState(Long id, Long state) {

        Announcement announcement = new Announcement();
        announcement.setId(id);

        UpdateWrapper<Announcement> updateWrapper = new UpdateWrapper<>();
        if(state==0) {
            announcement.setAnnouncementState(1);
        }else if(state==1){
            announcement.setAnnouncementState(2);
        }else if (state==2){
            announcement.setAnnouncementState(1);
        }
        announcementMapper.updateById(announcement);

//        int rows = userMapper.update(null, updateWrapper);
        System.out.println("更新完成");
        return Result.okResult("更新成功");
    }

}
